package com.lagou.controller;

/**
 * @Authther: mrwu
 * @date:2021/3/24-03
 * @Description:com.lagou.controller
 */

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文件下载
 */
@RequestMapping("/downlload")
public class DownloadController {

    public String show(HttpServletRequest request, Model modle){

        //获取地址
        String upload = request.getServletContext().getRealPath("upload");

        return "sucess";
    }
    public String dowload(@RequestParam String filename, HttpServletRequest request, HttpServletResponse resp) throws Throwable{

        FileInputStream inputStream=null;//输入流
        ServletOutputStream outputStream=null;//输出流
         //1获取要下载的文件路径
        String upload = request.getServletContext().getRealPath("upload");
        String filePath = upload+filename;
        inputStream = new FileInputStream(filePath);//从服务器本地文件中读出
        outputStream = resp.getOutputStream();
        int aread =0;
        byte temp[] =new byte[1024];
        while ((aread=inputStream.read(temp)) !=-1 &inputStream!=null){
            outputStream.write(temp);
        }
        outputStream.flush();
        inputStream.close();//需要关闭
        outputStream.close();
        return "dowload";
    }


}
